<template>
  <div class="login-container">
    <van-nav-bar title="黑马头条-登录">
      <template #left>
        <img class="logo" src="../../assets/images/logo.png" alt="">
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="rules.tel"
      />
      <van-field
        v-model="formLogin.code"
        type="password"
        label="密码"
        placeholder="请输入密码"
        required
        :rules="rules.password"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" :loading='isLoding' loading-text="正在拼命加载中...">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Notify } from 'vant'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      // 账号密码
      formLogin: {
        mobile: '13888888888',
        code: '246810' // 默认只能用246810后台规定了
      },
      // 表单验证
      rules: {
        password: [{
          required: true,
          message: '请填写密码',
          trigger: 'onChange'
        }, {
          message: '请填写密码',
          trigger: 'onChange',
          pattern: /^\d{6}$/
        }],

        tel: [{
          required: true,
          message: '请填写手机号',
          trigger: 'onChange'
        }, {
          message: '请填写手机号',
          trigger: 'onChange',
          pattern: /^1[3-9]\d{9}$/
        }]
      },
      // 加载状态
      isLoding: false
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmit () {
      try {
        this.isLoding = true
        await this.asyncLoginAction(this.formLogin)
        Notify({ type: 'success', message: '登录成功' })
        this.$router.replace({
          path: this.$route.query.from || '/layout'
        })
      } catch (e) {
        Notify({ type: 'warning', message: '手机号或密码错误' })
      } finally {
        this.isLoding = false
      }
    }
  }
}
</script>

<style lang="less" scoped>

.logo{
  width: 100px;
  height: 30px;
}

</style>
